<?php


defined('IN_PHPCMS') or exit('No permission resources.');
pc_base::load_app_class('admin','admin',0);
class teacherinfo extends admin {

	private $teacherinfo_db; 

	public $username;
	public function __construct() {
		parent::__construct();
		//if (!module_exists(ROUTE_M)) showmessage(L('module_not_exists'));
		$this->username = param::get_cookie('admin_username');
		$this->teacherinfo_db = pc_base::load_model('teacherinfo_model');
		pc_base::load_sys_class('form', '', 0);
		pc_base::load_sys_class('format', '', 0);

	}

public function init() {
		$username = param::get_cookie('admin_username');
		$userid = param::get_cookie('userid');

		$sql = '';
		$_GET['status'] = $_GET['status'] ? intval($_GET['status']) : 1;
		$_GET['menuid'] = $_GET['menuid'] ? intval($_GET['menuid']) : 1;
		$menuid=$_GET['menuid'];
		$_SESSION['menuid'] = $_GET['menuid'];
		$type = array("未选择","汽车检测与维修技术","汽车技术服务与营销","汽车制造与装配技术","汽车电子技术","机动车保险实务（查勘定损方向）","工程机械运用与维护");
		$page = max(intval($_GET['page']), 1);
		$data = $this->teacherinfo_db->listinfo($sql, '`id` ASC', $page, 15);
		$big_menu = array('javascript:window.top.art.dialog({id:\'add\',iframe:\'?m=teacherinfo&c=teacherinfo&a=add\', title:\''."添加".'\', width:\'700\', height:\'500\', lock:true}, function(){var d = window.top.art.dialog({id:\'add\'}).data.iframe;var form = d.document.getElementById(\'dosubmit\');form.click();return false;}, function(){window.top.art.dialog({id:\'add\'}).close()});void(0);', "添加");
		include $this->admin_tpl('teacherinfo_list');
	}
	
	public function add() {
		if(isset($_POST['dosubmit'])) {
			$_POST['teacherinfo'] = $this->check($_POST['teacherinfo']);
			if($this->teacherinfo_db->insert($_POST['teacherinfo'])) showmessage("教师信息成功录入", HTTP_REFERER, '', 'add');
		} else {
			//获取站点模板信息
			pc_base::load_app_func('global', 'admin');
			$siteid = $this->get_siteid();
			$template_list = template_list($siteid, 0);
			$site = pc_base::load_app_class('sites','admin');
			$info = $site->get_by_id($siteid);
			foreach ($template_list as $k=>$v) {
				$template_list[$v['dirname']] = $v['username'] ? $v['username'] : $v['dirname'];
				unset($template_list[$k]);
			}
			$show_header = $show_validator = $show_scroll = 1;
			pc_base::load_sys_class('form', '', 0);
			include $this->admin_tpl('teacherinfo_add');
		}
	}
	
	private function check($data = array(), $a = 'add') { 
		if($data['username']=='') showmessage("姓名不能为空");
		if($data['number']=='') showmessage("学号不能为空");
		if($data['number']=='0') showmessage("学号不能为0");
		if($data['major']=='') showmessage("专业不能为空");
		if($data['password']=='') showmessage("密码不能为空");
		if($data['type']=='0') showmessage("未选择分属部门");		
		if (!ereg("^[0-9]+$",$data['number'])){ 
              showmessage("学号应为数字！");}
		// if (!ereg("^[1-6]+$",$data['type'])){ 
              // showmessage("类型应为数字1——6！");}
		$r = $this->teacherinfo_db->get_one(array('number' => $data['number']));
		if ($a=='add') {
			if (is_array($r) && !empty($r)) {
				showmessage("该教师信息已存在", HTTP_REFERER);
			}
		} else {
			if ($r['id'] && ($r['id']!=$_GET['id'])) {
				showmessage("该教师信息已存在", HTTP_REFERER);
			}
		}
		return $data;
	}
	
	
	public function search() {
		$username = param::get_cookie('admin_username');
		$userid = param::get_cookie('userid');
		$_GET['status'] = $_GET['status'] ? intval($_GET['status']) : 1;
		$_GET['menuid'] = $_GET['menuid'] ? intval($_GET['menuid']) : 1;
		$menuid=$_GET['menuid'];
		$name=isset($_GET['name'])?$_GET['name']:''; 
		$number=isset($_GET['number'])?$_GET['number']:'';
		$major=isset($_GET['major'])?$_GET['major']:'';
		$batch=isset($_GET['batch'])?$_GET['batch']:'';
		//showmessage($major);
		$name = trim($name);
		$number = trim($number);
		$major = trim($major);
		$batch = trim($batch);
		$where='';
		if(!empty($name)){
			$where .= "`username` like '%$name%' AND ";
		}
		if(!empty($number)){
			$where .= "`number` = '$number'  AND ";
		}
		if(!empty($major)){
			$where .= "`major` like '%$major%'  AND ";
		}
		if(!empty($batch)){
			$where .= "`batch` = '$batch'  AND ";
		}
		$where .= '`id`!=\'0\'';	 //关键点
		$order = 'id asc';
		$type = array("未选择","汽车检测与维修技术","汽车技术服务与营销","汽车制造与装配技术","汽车电子技术","机动车保险实务（查勘定损方向）","工程机械运用与维护");
		$page = isset($_GET['page']) && intval($_GET['page']) ? intval($_GET['page']) : 1;
		$data = $this->teacherinfo_db->listinfo($where,$order, $page, 10);
		$big_menu = array('?m=teacherinfo&c=teacherinfo&a=init&menuid='.$_SESSION['menuid'].'&s=1',"刷新");
		include $this->admin_tpl('teacherinfo_list');
	}
	
	
	
		public function edit() {
		$_GET['id'] = intval($_GET['id']);//showmessage($_GET['id']);
		if(!$_GET['id']) showmessage(L('illegal_operation'));
		if(isset($_POST['dosubmit'])) {
			$_POST['teacherinfo'] = $this->check($_POST['teacherinfo'], 'edit');
			if($this->teacherinfo_db->update($_POST['teacherinfo'], array('id' => $_GET['id'])))
			showmessage(L('tuangoud_a'), 
			HTTP_REFERER, '', 'edit');
		} else {
			$where = array('id' => $_GET['id']);
			$an_info = $this->teacherinfo_db->get_one($where);//showmessage($an_info);
			pc_base::load_sys_class('form', '', 0);
			//获取站点模板信息
			pc_base::load_app_func('global', 'admin');
			$template_list = template_list($this->siteid, 0);
			foreach ($template_list as $k=>$v) {
				$template_list[$v['dirname']] = $v['name'] ? $v['name'] : $v['dirname'];
				unset($template_list[$k]);
			}
			$show_header = $show_validator = $show_scroll = 1;
			$big_menu = array('?m=teacherinfo&c=teacherinfo&a=init&menuid='.$_GET['menuid'].'&s=1',"刷新");
			include $this->admin_tpl('teacherinfo_edit');
		}
	}
	

	
	/**
	 * 批量删除信息
	 */
	public function delete($id = 0) {
		if((!isset($_POST['id']) || empty($_POST['id'])) && !$id && (!isset($_GET['id']) || empty($_GET['id'])) ) {
			showmessage(L('illegal_operation'));
		} else if(isset($_POST['id'])){
			if(is_array($_POST['id']) && !$id) {
				array_map(array($this, 'delete'), $_POST['id']);
				showmessage("成功删除所选信息", HTTP_REFERER);
			} elseif($id) {
				$id = intval($id);
				$this->teacherinfo_db->delete(array('id' => $id));
			}
		}
		else{
				$this->teacherinfo_db->delete(array('id' => $_GET['id']));
				showmessage("成功删除所选信息");
		}
	}	 
	
	
	
	
	public function import(){
		if(isset($_POST['dosubmit'])) {
				$info = array();
				$info['database']=$_POST['info']['database'];
				$info['username']=$_POST['info']['username'];
				$info['password']=$_POST['info']['password'];
				require_once 'reader.php'; // ExcelFile($filename, $encoding); 
				$data = new Spreadsheet_Excel_Reader(); // Set output Encoding. 
				$data->setOutputEncoding('CP936');
				$data->read($_FILES['model_import']['tmp_name']); 
				@ $db = mysql_connect($info['database'], $info['username'], $info['password']) or 
				die("Could not connect to database.");//连接数据库 
				mysql_query("set names 'gbk'");//输出中文 
				mysql_select_db('mydb'); //选择数据库 
				error_reporting(E_ALL ^ E_NOTICE); 
				for ($i = 1; $i <= $data->sheets[0]['numRows']; $i++) { 
				//以下注释的for循环打印excel表数据 
				/* 
			for ($j = 1; $j < = $data->sheets[0]['numCols']; $j++) { 
			echo "\"".$data->sheets[0]['cells'][$i][$j]."\","; 
			} 
			echo "\n"; 
			*/ 
			 if ($data->sheets[0]['cells'][$i][1] == '')showmessage("姓名不能为空");
			 if ($data->sheets[0]['cells'][$i][2] == '')showmessage("学号不能为空");
			 if ($data->sheets[0]['cells'][$i][2] == '0')showmessage("学号不能为0");
			 if ($data->sheets[0]['cells'][$i][3] == '')showmessage("密码不能为空");
			 if ($data->sheets[0]['cells'][$i][4] == '')showmessage("专业不能为空");
			 if ($data->sheets[0]['cells'][$i][5] == '')showmessage("分属部门不能为空");
			 if ($data->sheets[0]['cells'][$i][5] == '')showmessage("批次不能为空");
			 if (!ereg("^[0-9]+$",$data->sheets[0]['cells'][$i][2])){ showmessage("学号只能为数字，学号数据格式有错");}
			 if (!ereg("^[1-6]+$",$data->sheets[0]['cells'][$i][5])){ showmessage("类型只能为数字0——6，类型数据格式有错");}
			//showmessage($data->sheets[0]['cells'][$i][1]);
				//以下代码是将excel表数据【4个字段】插入到mysql中， 
				//根据你的excel表字段的多少，改写以下代码吧！ 
				$sql = "INSERT INTO v9_teacherinfo VALUES('". 
				''."','". 
				$data->sheets[0]['cells'][$i][1]."','". 
				$data->sheets[0]['cells'][$i][2]."','". 
				$data->sheets[0]['cells'][$i][3]."','". 
				$data->sheets[0]['cells'][$i][4]."','". 
				$data->sheets[0]['cells'][$i][5]."','". 
				$data->sheets[0]['cells'][$i][6]."')"; 
				//echo $sql.'< br />'; 
				$res = mysql_query($sql); 
				//
				} showmessage("成功添加数据到数据库");}
			 else {
			pc_base::load_sys_class('form','',0);
			
			include $this->admin_tpl('teacherinfo_import');
		}
		}

		
		
	
}
?>
